package com.sztx.pay.center.rpc.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.Constance.TradeOrderType;
import com.sztx.pay.center.dataaccess.dao.mysql.PayFlowMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.PayFlowDO;
import com.sztx.pay.center.rpc.api.domain.PayFlowRequest;
import com.sztx.pay.center.rpc.api.domain.PayFlowResponse;
import com.sztx.pay.center.rpc.api.domain.PayFlowResponse.PayFlow;
import com.sztx.pay.center.rpc.api.service.PayFlowRpcService;
import com.sztx.se.common.util.date.DateUtil;
import com.sztx.se.core.service.impl.BaseServiceImpl;
import com.sztx.se.dataaccess.mysql.config.PageQuery;
@Service
public class PayFlowRpcServiceImpl extends BaseServiceImpl implements PayFlowRpcService{
	@Autowired
	PayFlowMysqlDAO payFlowMysqlDAO;
	
	@Override
	@SlaveQueryAnnotation
	public PayFlowResponse findUserPayFlow(PayFlowRequest request) {
		
		PageQuery pageQuery = new PageQuery(request.getPageNo(), request.getPageSize());
		String userId = request.getUserId();
		String endTime = DateUtil.formatDate(request.getEndDate(), DateUtil.LONG_DATE_FORMAT_STR);
		String startTime = DateUtil.formatDate(request.getStartDate(), DateUtil.LONG_DATE_FORMAT_STR);
		PayFlowDO payFlowDO = new PayFlowDO();
		payFlowDO.setPayerUserId(request.getPayerUserId());
		payFlowDO.setPayeeUserId(request.getPayeeUserId());
		payFlowDO.setTradeOrderNo(request.getTradeOrderNo());
		payFlowDO.setTradeOrderType(request.getTradeOrderType());
		List<PayFlowDO> payFlowDOS = payFlowMysqlDAO.findUserPayFlow(payFlowDO, startTime, endTime, userId, pageQuery);
		PayFlowResponse response = new PayFlowResponse();
		Integer counts = payFlowMysqlDAO.findUserPayFlowCount(payFlowDO, startTime, endTime, userId);
		counts = counts == null ? 0:counts;
		
		int inAmount = 0;
		int outAmount = 0;
		if(request.getUserId() == null){
			if(request.getPayerUserId() != null){
				Integer tmpoutAmount = payFlowMysqlDAO.findUserPayFlowAmount(payFlowDO, startTime, endTime, userId);
				outAmount = tmpoutAmount == null ? 0 : tmpoutAmount;
				inAmount = 0;
			}else if(request.getPayeeUserId() != null){
				Integer tmpinAmount = payFlowMysqlDAO.findUserPayFlowAmount(payFlowDO, startTime, endTime, userId);
				inAmount = tmpinAmount == null ? 0 : tmpinAmount;
				outAmount = 0;
			}
			
		}else{
			PayFlowDO payFlowDO2 = new PayFlowDO();
			payFlowDO2.setPayerUserId(request.getUserId());
			payFlowDO2.setPayeeUserId(null);
			payFlowDO2.setTradeOrderNo(request.getTradeOrderNo());
			payFlowDO2.setTradeOrderType(request.getTradeOrderType());
			Integer tmpoutAmount = payFlowMysqlDAO.findUserPayFlowAmount(payFlowDO2, startTime, endTime, null);
			outAmount = tmpoutAmount == null ? 0 : tmpoutAmount;
			
			payFlowDO2.setPayerUserId(null);
			payFlowDO2.setPayeeUserId(request.getUserId());
			Integer tmpinAmount = payFlowMysqlDAO.findUserPayFlowAmount(payFlowDO2, startTime, endTime, null);
			inAmount = tmpinAmount == null ? 0 : tmpinAmount;
			
		}
		List<PayFlow> payFlows = new ArrayList<PayFlow>();
		
		for (PayFlowDO payFlowdo : payFlowDOS) {
			PayFlow payFlow = new PayFlowResponse().new PayFlow();
			payFlow.setAmount(payFlowdo.getAmount());
			payFlow.setPayeeUserId(payFlowdo.getPayeeUserId());
			payFlow.setPayerUserId(payFlowdo.getPayerUserId());
			TradeOrderType tradeOrderType = TradeOrderType.valueOf(payFlowdo.getTradeOrderType());
			if(tradeOrderType == TradeOrderType.REFUND_ORDER){
				payFlow.setTradeOrderNo(payFlowdo.getTradeOrderNo());
			}else if(tradeOrderType == TradeOrderType.SETTLE_ORDER){
				payFlow.setTradeOrderNo(payFlowdo.getBatchOrderId());
			}else if(payFlowdo.getMappingTradeOrderId() != null){
				payFlow.setTradeOrderNo(payFlowdo.getMappingTradeOrderId());
			}else{
				payFlow.setTradeOrderNo(payFlowdo.getTradeOrderNo());
			}
			
			payFlow.setTradeOrderType(payFlowdo.getTradeOrderType());
			payFlow.setPayDate(payFlowdo.getPayTime());
			payFlows.add(payFlow);
		}
		
		
		
		response.setInAmount(inAmount);
		response.setOutAmount(outAmount);
		response.setPageNo(request.getPageNo());
		response.setPageSize(request.getPageSize());
		response.setPayFlows(payFlows);
		response.setTotal(counts);
		
		
		return response;
		
	}

}
